package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.GoodsEvaluateListAdapter;
import com.longcai.piaojia.conn.EvaluateListGet;
import com.longcai.piaojia.conn.EvaluateZanGet;
import com.longcai.piaojia.conn.GoodsInfoGet;
import com.longcai.piaojia.util.MyToast;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/8/2.
 */
public class GoodsEvaluateListActivity extends BaseActivity implements View.OnClickListener, GoodsEvaluateListAdapter.EvaluateZanListener {

    @BoundView(R.id.goods_evaluate_list_lv) private PullToRefreshListView  pullToRefreshListView;

    private ListView goods_evaluate_list_lv;
    private GoodsEvaluateListAdapter goodsEvaluateListAdapter;
    private List<EvaluateListGet.EvaluateEntity> lists = new ArrayList<>();
    private GoodsInfoGet.GoodsInfoEntity goodsInfo;
    private int position = -1;
    private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
    private EvaluateListGet.EvaluateListEntity evaluateList;

    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        pullToRefreshListView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        return mDateFormat.format(new Date(time));
    }

    private EvaluateListGet evaluateListGet = new EvaluateListGet(new AsyCallBack<EvaluateListGet.EvaluateListEntity>(){

        @Override
        public void onEnd(String toast, int type) throws Exception {
            super.onEnd(toast, type);

            pullToRefreshListView.onPullDownRefreshComplete();
            pullToRefreshListView.onPullUpRefreshComplete();
            setLastUpdateTime();

        }

        @Override
        public void onSuccess(String toast, int type, EvaluateListGet.EvaluateListEntity evaluateListEntity) throws Exception {
            super.onSuccess(toast, type, evaluateListEntity);

//            if(evaluateListEntity.state.equals("0")){
//
//                setRightName(getResources().getString(R.string.goods_evaluate_list_write_evaluate_text), R.color.white, GoodsEvaluateListActivity.this);
//
//            }

            evaluateList = evaluateListEntity;

            lists.addAll(evaluateListEntity.list);
            goodsEvaluateListAdapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(GoodsEvaluateListActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });

    private EvaluateZanGet evaluateZanGet = new EvaluateZanGet(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {
            super.onSuccess(toast, type, s);

            lists.get(position).zambia = String.valueOf(Integer.parseInt(lists.get(position).zambia)+1);

            lists.get(position).zambia_state = "1";

            goodsEvaluateListAdapter.notifyDataSetChanged();

            MyToast.show(GoodsEvaluateListActivity.this, toast, Toast.LENGTH_LONG);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            MyToast.show(GoodsEvaluateListActivity.this, toast, Toast.LENGTH_LONG);

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_goods_evaluate_list);

        goodsInfo = (GoodsInfoGet.GoodsInfoEntity) getIntent().getSerializableExtra("goodsInfo");

        setBackTrue();

        setTitleName(getResources().getString(R.string.goods_evaluate_list_title_text));

        pullToRefreshListView.setScrollLoadEnabled(false);
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setPullRefreshEnabled(true);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                lists.clear();

                evaluateListGet.show_id = goodsInfo.id;
                evaluateListGet.user_id = BaseApplication.BasePreferences.readUserId();
                evaluateListGet.page = 1;
                evaluateListGet.execute(GoodsEvaluateListActivity.this, false);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if(evaluateList!=null&&evaluateList.total!=lists.size()){

                    evaluateListGet.show_id = goodsInfo.id;
                    evaluateListGet.user_id = BaseApplication.BasePreferences.readUserId();
                    evaluateListGet.page = evaluateList.current_page + 1;
                    evaluateListGet.execute(GoodsEvaluateListActivity.this, false);

                }else{

                    pullToRefreshListView.onPullDownRefreshComplete();
                    pullToRefreshListView.onPullUpRefreshComplete();

                }

            }
        });

        goods_evaluate_list_lv = pullToRefreshListView.getRefreshableView();

        goods_evaluate_list_lv.setDividerHeight(ScaleScreenHelperFactory.getInstance().getWidthHeight(1));

        goodsEvaluateListAdapter = new GoodsEvaluateListAdapter(this, lists, this);
        goods_evaluate_list_lv.setAdapter(goodsEvaluateListAdapter);

        goods_evaluate_list_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(GoodsEvaluateListActivity.this, GoodsEvaluateDetailActivity.class).putExtra("show_id", goodsInfo.id).putExtra("user_id", lists.get(position).user_id).putExtra("zambia_state", lists.get(position).zambia_state).putExtra("posttime", lists.get(position).posttime).putExtra("position", position));

            }
        });

        setAppCallBack(new GoodsEvaluateCallBack());

        evaluateListGet.show_id = goodsInfo.id;
        evaluateListGet.user_id = BaseApplication.BasePreferences.readUserId();
        evaluateListGet.page = 1;
        evaluateListGet.execute(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.title_right_ll:

                if (!BaseApplication.BasePreferences.readIsLogin()){

                    startActivity(new Intent(GoodsEvaluateListActivity.this, LoginActivity.class));

                }else{

                    startActivity(new Intent(GoodsEvaluateListActivity.this, GoodsEvaluateActivity.class).putExtra("goodsInfo", goodsInfo));

                }

                break;

        }

    }

    @Override
    public void setZan(int pos) {

        if (!BaseApplication.BasePreferences.readIsLogin()){

            startActivity(new Intent(GoodsEvaluateListActivity.this, LoginActivity.class));

        }else{

            position = pos;

            evaluateZanGet.member_id = BaseApplication.BasePreferences.readUserId();
            evaluateZanGet.show_id = goodsInfo.id;
            evaluateZanGet.user_id = lists.get(pos).user_id;
            evaluateZanGet.zambia = lists.get(pos).zambia;
            evaluateZanGet.posttime = lists.get(pos).posttime;

            evaluateZanGet.execute(GoodsEvaluateListActivity.this);

        }

    }

    public class GoodsEvaluateCallBack implements AppCallBack{

        public void refresh(){

            lists.clear();

            evaluateListGet.show_id = goodsInfo.id;
            evaluateListGet.user_id = BaseApplication.BasePreferences.readUserId();
            evaluateListGet.execute(GoodsEvaluateListActivity.this);

        }

        public void refresh_zan(int pos){

            lists.get(pos).zambia = String.valueOf(Integer.parseInt(lists.get(pos).zambia)+1);
            lists.get(pos).zambia_state = "1";
            goodsEvaluateListAdapter.notifyDataSetChanged();

        }

    }

}
